package com.logic.lhub.main;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.logic.lhub.R;
import com.logic.lhub.databinding.DelegateIndexHomeBinding;
import com.qsmx.core.delegates.AtmosDelegate;

import java.util.ArrayList;
import java.util.List;

public class IndexHomeDelegate extends AtmosDelegate {

    private DelegateIndexHomeBinding mBinding;

    @Override
    public Object setLayout() {
        return R.layout.delegate_index_home;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

        mBinding = DelegateIndexHomeBinding.bind(rootView);
        mBinding.vpHome.startAutoScroll(5000);
        mBinding.vpHome.setInterval(5000);
        mBinding.vpHome.setCycle(true);
        setupViewPager(mBinding.vpHome);
    }

    private void setupViewPager(ViewPager viewPager) {
        MyAdapter adapter = new MyAdapter(getChildFragmentManager());
        adapter.addFragment(new IndexMachineHomeDelegate(), "1");
        adapter.addFragment(new IndexMachineHomeDelegate(), "2");
        adapter.addFragment(new IndexMachineHomeDelegate(), "3");
        adapter.addFragment(new IndexMachineHomeDelegate(), "4");
        viewPager.setAdapter(adapter);
    }

    class MyAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public MyAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

}
